<template>
<div class="direct-select">
    <!-- 选球区域 -->
    <div class="game-area">
        <div class="game-area-forward">
            <div class="game-area-forward-title">
                <div class="game-area-forward-title-text">
                    <text class="game-area-forward-title-tips">选</text>
                    <text class="game-area-forward-title-tips" style="color: #E70D0D; font-size:27px;margin-left:4px;margin-right:4px;">1</text>
                    <text class="game-area-forward-title-tips">个单号，一个</text>
                    <text class="game-area-forward-title-tips" style="color: #E70D0D; font-size:27px;margin-left:4px;margin-right:4px;">1</text>
                    <text class="game-area-forward-title-tips">重号</text>
                </div>
                
                <!-- @click="shakephone" -->
                <image class="game-area-forward-title-shake" src="http://zhongcai-test.oss-cn-beijing.aliyuncs.com/test-huicai-admin/shakep.png" @click="shake"></image>
            </div>
            <ball-list
                class="ball-list-demo" 
                v-for="(item,index) in items" :key="index"
                type="simplex"
                :defaultValue = defaultBall[index]
                :ball-section="[0,9]"
                :ballLenth="5"
                :ball-title="item.title"
                :identification="item.name"
                :errorCallbak="judgeFn"
                @castBallList="getBallList"
            ></ball-list>
        </div>
    </div>
    <!-- 选球区域 -->
</div>
</template>
<script>
import ballList from "@/components/lotteries/components/ballList"

const modal = weex.requireModule("modal")

export default {
    data() {
        return {
            items:[{'title':'重号', 'name': 'double'}, {'title':'单号', 'name': 'odd'}],
            defaultBall:[],
            total:{},
            selectBall:{
                double:[],
                odd:[],
            },

            curBall:{
                double:[],
                odd:[],
            }
        }
    },

    watch:{
        selectBall:{
            handler (val, oldVal) {
                this.count()
            },
            deep: true
        },
    },

    methods:{
        // 获取普通号的选值
        getBallList(value, name, key) {
            this.selectBall[name] = value
            this.curBall[name] = value
        },

        // 计算金额和注数
        count() {
            // 获取各位置选区的注数
            let iarr = Object.values(this.selectBall).map((item)=> item.length )
            // 如果含有未选中的 不返回金额
            if(iarr.indexOf(0) > -1) {
                this.$emit('setTotal', {})
                return false;
            }

            // 计算所选注数
            let num  = iarr.reduce((accumulator, currentValue)=> accumulator * currentValue)

            this.total = {
                chong   : this.selectBall['double'],
                dan     : this.selectBall['odd'],
                numberType: 2,
                type    : "basisM",
                number  : num ,
                money : num * 2
            }

            this.$emit('setTotal', this.total, this.selectBall)
        },
        
        // 错误判断
        judgeFn(param) {
            let { name, ball, value } = param

            for(let key in this.curBall) {
                if( key != name && this.curBall[key].indexOf(value) > -1) {
                    modal.toast({ message: `单式和重号两个号码不能相同`}, 0.5)
                    return false
                } 
            }

            this.curBall[name] = ball
            
            return true
        },

        // 摇一摇随机一组
        shake() {
            this.defaultBall = this.createRandowArr(0, 9, 2)
        },

        // 随机数值
        createRandowArr(min, max, length) {
            let r = [],
                o = {},
                a;

            for (var i = 0; i < length; i++) {
                a = Math.floor(min + max * Math.random())

                o[a] ? i-- : (r.push(a), (o[a] = true))
            }

            return r.sort((a, b) => (a < b ? -1 : 1))
        },

        // 随机指定几组数值
        randowFn(len) {
            let iarr = [],
                obj = {};
                
            for(var i = 0; i < len; i++) {
                iarr.push(this.createRandowArr(0, 9, 2))
            }
            
            return iarr
        },

        // 随机返回数值
        randowParam(len) {
            let iarr = this.randowFn(len),
                paramIarr = []
            
            iarr.forEach((v,i)=>{
                paramIarr.push({
                    "chong": [v[0]],
                    "dan": [v[1]],
                    "numberType": "2",
                    "type": "multiple",
                    "money": 2,
                    "number": 1
                })
            })

            return paramIarr
        }
    },

    components:{
        ballList
    }
}
</script>
<style scoped>
/* game-area */
span {
  align-self: center;
  font-weight: bold;
}
.game-area-forward-title-text {
  flex-direction: row;
}
.game-area-forward-title {
	flex-direction: row;
	justify-content: space-between;
	height: 48px;
	display: flex;
	align-items: center;
	margin-bottom:32px;
}
.game-area-forward-title-shake {
    width: 44px;
    height: 44px;
}

.game-area-forward {
  padding-top: 30px;
  padding-bottom: 10px;
  padding-left: 32px;
  padding-right: 46px;
  width:750px;
}

.game-area-forward-title-tips {
	align-self: center;
	font-family: PingFangSC-Semibold;
	font-size: 24px;
	color: #333333;
}

.ball-list-demo {
	margin-bottom:64px;	
}

/* game-area */
</style>




